<?php

/**
 * This is the model class for table "ykl_company".
 *
 * The followings are the available columns in table 'ykl_company':
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $title
 * @property string $description
 * @property string $logo
 * @property integer $hit
 * @property integer $point
 */
class Company extends CActiveRecord
{
	public $oldpassword;
	public $newpassword;
	public $respassword; 
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Company the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ykl_company';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, title, description,address,tel,template', 'required','message'=>'不能为空'),
			array('hit', 'numerical', 'integerOnly'=>true),
			array('username, password, title,address,tel,portalurl', 'length', 'max'=>128),
			array('username', 'unique', 'message'=>'用户名称重复'),  
			//验证企业logo的上传条件 
		 	array('logo',
	            'file', //定义为file类型
	            'allowEmpty' => true,
	            'types' => 'jpg,png,gif', //上传文件的类型
	            'maxSize' => 1024 * 1024 * 3, //上传大小限制，注意不是php.ini中的上传文件大小
	            'tooLarge' => '文件大于3M，上传失败！请上传小于3M的文件！'
            ),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username, password, title, description, logo, hit, point,address,tel , portalurl , template', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			//'blacklists'=>array(self::HAS_MANY,'CompanyBlacklist','companyid'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'username' => '用户名',
			'password' => '密码',
			'title' => '企业名称',
			'description' => '介绍',
			'logo' => '企业logo',
			'hit' => '点击量',
			'point' => '短信条数',
			'address' => '企业地址',
			'tel' => '联系电话',
			'template' => '首页模板',
			'oldpassword' => '旧密码',
			'newpassword' => '新密码',
			'respassword' => '确认密码',
			'portalurl'=>'登陆成功跳转页面', 
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('logo',$this->logo,true);
		$criteria->compare('address',$this->address,true);
		$criteria->compare('tel',$this->tel);
		$criteria->compare('template',$this->template);
		$criteria->compare('hit',$this->hit);
		$criteria->compare('point',$this->point);
		$criteria->order="id desc";

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * 判断是否有钱能够支持短信消费
	 * @param string $companyid 企业id $count 短信条数,默认为1.
	 * @return void
	 * @author 
	 **/
	public function isSend($companyid,$count='1')
	{
		$model=$this->model()->find("id=:id",array(":id"=>$companyid));
		if(!empty($model)){
			$countpoint = intval($count);
			return ($model->point - $countpoint)>=0 ?true:false; 
		}
		return false; 
	}
	/**
	 * 根据企业id,以及发送条数,扣除企业充值费用 
	 * @param string $companyid 企业id $count 短信条数,默认为1.
	 * @return void 
	 **/  
	public function deductions($companyid,$count='1'){
		$model=$this->model()->find("id=:id",array(":id"=>$companyid));
		if(!empty($model)){ 
			$countpoint = intval($count);
			$model->point=$model->point - $countpoint;
			$model->save();  
		}
	} 
	 

	 /**
	  * 得到充值信息 
	  **/
	 public function getSmsfree()
	 {
	 	 $freearray = array(
          '100'=>'1000条(100元)',
          '450'=>'5000条(<span class="candara">9折</span> 折后价450元)',
          '800'=>'10000条(<span class="candara">8折</span> 折后价800元)',
          '6000'=>'100000条(<span class="candara">6折</span> 折后价6000元)',
          );

	 	 return $freearray; 
	 }

	  /**
	  * 得到条数
	  **/
	 public function getSmsCount($type)
	 {
	 	 $freearray = array(
          '100'=>'1000',
          '450'=>'5000',
          '800'=>'10000',
          '6000'=>'100000',
          );
	 	 $returnlen='0';
	 	 foreach ($freearray as $key => $value) { 
	 	 	if($key==$type){ 
	 	 		$returnlen=$value;
	 	 		break; 
	 	 	}
	 	 } 
	 	 return $returnlen;
	 }
	 /**
     * 得到特定的模板 
     **/ 
    public function getTemplates($id){
        $template=array(
                array("id"=>0,"title"=>"无首页"),
                array("id"=>1,"title"=>"视频广告"),
                array("id"=>2,"title"=>"全屏轮播图"),
                array("id"=>10,"title"=>"企业微门户"),
                array("id"=>13,"title"=>"企业微门户(医疗)"),
                array("id"=>14,"title"=>"企业微门户(4s店)"),
                array("id"=>11,"title"=>"微信关注登陆"),
                array("id"=>12,"title"=>"应用下载"), 
            );
        if($id==""){
             return  $template;
        }else{
            foreach ($template as $key => $value) {
               if($value['id']==$id){
                return $value['title']; 
               }
            }
        }
       
    }


}